<?php
class Me {
     protected  $name;
     protected  $pwd;
     protected  $dbh;
     public  function  __construct($name,$pwd)
     {
         $this->name=$name;
         $this->pwd=$pwd;
         $this->dbh = new PDO('mysql:host=localhost;dbname=three', $this->name, $this->pwd);
     }
    //查询一条
    public function getOne($table,$where)
    {
            $sql="select * from $table where $where";
            $result=$this->dbh->query($sql);
            $res=$result->fetch(2);
            return $res;
    }
    //查询多条
    public function getAll($table)
    {
        $sql="select * from $table";
        $result=$this->dbh->query($sql);
        $res=$result->fetchAll(2);
        return $res;
    }
    //删除
    public function delOne($table,$where)
    {
        if(empty($table) && empty($where)){
            echo "条件缺一不可";
           return false;
        }
        $sql="delete from $table where $where";
        $result=$this->dbh->exec($sql);
        return $result;
    }
    //修改
    public function upOne($table,$arr,$where)
    {
        if(empty($arr)){
            echo "数据不能为空";
            return false;
        }
        if(empty($table) || empty($where)){
            echo "条件缺一不可";
            return false;
        }
        $result='';
        foreach($arr as $key => $value){
            $result.=",$key"."="."'$value'";
        }
        //截取
        $result=substr($result,1);
        //sql
        $sql="update  user  set $result where $where";
        $res=$this->dbh->exec($sql);
        return $res;
    }
    //添加
    public function addOne($table,$arr)
    {
        if(empty($arr || $table)){
            echo "数据不能为空";
            return false;
        }
        $res='';
        $req='';
        foreach($arr as $key => $value){
            $res.=",$key";
            $req.=",'$value'";
        }
        //截取
        $res=substr($res,1);
        $req=substr($req,1);
        //sql
        $sql="insert into user ($res) value ($req)";
        //预处理
        $data=$this->dbh->prepare($sql);
        //执行
        $res=$data->execute();
        return $res;
    }
}

